{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bayes by Backprop from scratch (NN, classification)\n",
    "\n",
    "We have already learned how to implement deep neural networks and how to use them for classification and regression tasks. In order to fight overfitting, we further introduced a concept called _dropout_, which randomly turns off a certain percentage of the weights during training.\n",
    "\n",
    "Recall the classic architecture of a MLP (shown below, without bias terms). So far, when training a neural network, our goal was to find an optimal point estimate for the weights.\n",
    "\n",
    "![](https://github.com/zackchase/mxnet-the-straight-dope/blob/master/img/bbb_nn_classic.png?raw=true)\n",
    "\n",
    "While networks trained using this approach usually perform well in regions with lots of data, they fail to express uncertainity in regions with little or no data, leading to overconfident decisions. This drawback motivates the application of Bayesian learning to neural networks, introducing probability distributions over the weights. These distributions can be of various nature in theory. To make our lifes easier and to have an intuitive understanding of the distribution at each weight, we will use a Gaussian distribution.\n",
    "\n",
    "![](https://github.com/zackchase/mxnet-the-straight-dope/blob/master/img/bbb_nn_bayes.png?raw=true)\n",
    "\n",
    "Unfortunately though, exact Bayesian inference on the parameters of a neural network is intractable. One promising way of addressing this problem is presented by the \"Bayes by Backprop\" algorithm (introduced by the \"[Weight Uncertainity in Neural Networks](https://arxiv.org/abs/1505.05424)\" paper) which derives a variational approximation to the true posterior. This algorithm does not only make networks more \"honest\" with respect to their overall uncertainity, but also automatically leads to regularization, thereby eliminating the need of using dropout in this model.\n",
    "\n",
    "While we will try to explain the most important concepts of this algorithm in this notebook, we also encourage the reader to consult the paper for deeper insights."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's start implementing this idea and evaluate its performance on the MNIST classification problem. We start off with the usual set of imports."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import collections\n",
    "import mxnet as mx\n",
    "import numpy as np\n",
    "from mxnet import nd, autograd\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For easy tuning and experimentation, we define a dictionary holding the hyper-parameters of our model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "config = {\n",
    "    \"num_hidden_layers\": 2,\n",
    "    \"num_hidden_units\": 400, \n",
    "    \"batch_size\": 128,\n",
    "    \"epochs\": 10,\n",
    "    \"learning_rate\": 0.001,\n",
    "    \"num_samples\": 1,\n",
    "    \"pi\": 0.25,\n",
    "    \"sigma_p\": 1.0,\n",
    "    \"sigma_p1\": 0.75,\n",
    "    \"sigma_p2\": 0.1,\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Also, we specify the device context for MXNet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "ctx = mx.cpu()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load MNIST data\n",
    "\n",
    "We will again train and evaluate the algorithm on the MNIST data set and therefore load the data set as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def transform(data, label):\n",
    "    return data.astype(np.float32)/126.0, label.astype(np.float32)\n",
    "\n",
    "mnist = mx.test_utils.get_mnist()\n",
    "num_inputs = 784\n",
    "num_outputs = 10\n",
    "batch_size = config['batch_size']\n",
    "\n",
    "train_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=True, transform=transform),\n",
    "                                      batch_size, shuffle=True)\n",
    "test_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform),\n",
    "                                     batch_size, shuffle=False)\n",
    "\n",
    "num_train = sum([batch_size for i in train_data])\n",
    "num_batches = num_train / batch_size"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to reproduce and compare the results from the paper, we preprocess the pixels by dividing by 126."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Model definition\n",
    "\n",
    "### Activation function\n",
    "\n",
    "As with lots of past examples, we will again use the ReLU as our activation function for the hidden units of our neural network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def relu(X):\n",
    "    return nd.maximum(X, nd.zeros_like(X))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Neural net modeling\n",
    "\n",
    "As our model we are using a straightforward MLP and we are wiring up our network just as we are used to."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "num_layers = config['num_hidden_layers']\n",
    "\n",
    "# define function for evaluating MLP\n",
    "def net(X, layer_params):\n",
    "    layer_input = X\n",
    "    for i in range(len(layer_params) // 2 - 2):\n",
    "        h_linear = nd.dot(layer_input, layer_params[2*i]) + layer_params[2*i + 1]\n",
    "        layer_input = relu(h_linear)\n",
    "    # last layer without ReLU\n",
    "    output = nd.dot(layer_input, layer_params[-2]) + layer_params[-1]\n",
    "    return output\n",
    "\n",
    "# define network weight shapes\n",
    "layer_param_shapes = []\n",
    "num_hidden = config['num_hidden_units']\n",
    "for i in range(num_layers + 1):\n",
    "    if i == 0: # input layer\n",
    "        W_shape = (num_inputs, num_hidden)\n",
    "        b_shape = (num_hidden,)  \n",
    "    elif i == num_layers: # last layer\n",
    "        W_shape = (num_hidden, num_outputs)\n",
    "        b_shape = (num_outputs,)\n",
    "    else: # hidden layers\n",
    "        W_shape = (num_hidden, num_hidden)\n",
    "        b_shape = (num_hidden,)\n",
    "    layer_param_shapes.extend([W_shape, b_shape])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Build objective/loss"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As we briefly mentioned at the beginning of the notebook, we will use variational inference in order to make the prediction of the posterior tractable. While we can not model the posterior $P(\\mathbf{w}\\ |\\ \\mathcal{D})$ directly, we try to find the parameters $\\mathbf{\\theta}$ of a distribution on the weights $q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})$ (commly referred to as the _variational posterior_) that minimizes the KL divergence with the true posterior. Formally this can be expressed as:\n",
    "\n",
    "\\begin{equation*}\n",
    "\\begin{split}\n",
    "\\theta^{*} & = \\arg\\min_{\\theta} \\text{KL}[q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})\\ ||\\ P(\\mathbf{w}\\ |\\ \\mathcal{D}]\\\\\n",
    "& = \\arg\\min_{\\theta} \\int q(\\mathbf{w}\\ |\\ \\mathbf{\\theta}) \\log \\frac{q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})}{P(\\mathbf{w}) P(\\mathcal{D}\\ |\\ \\mathbf{w})} d\\mathbf{w} \\\\\n",
    "& = \\arg\\min_{\\theta} \\text{KL}[q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})\\ ||\\ P(\\mathbf{w})] - \\mathbb{E}_{q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})}[\\log P(\\mathcal{D}\\ |\\ \\mathbf{w})]\n",
    "\\end{split}\n",
    "\\end{equation*}\n",
    "\n",
    "The resulting loss function, commonly referred to as either _variational free energy_ or _expected lower bound_ (_ELBO_), has to be minimized and is then given as follows:\n",
    "\n",
    "\\begin{equation*}\n",
    "\\mathcal{F}(\\mathcal{D}, \\mathbf{\\theta}) = \\text{KL}[q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})\\ ||\\ P(\\mathbf{w})] - \\mathbb{E}_{q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})}[\\log P(\\mathcal{D}\\ |\\ \\mathbf{w})]\n",
    "\\end{equation*}\n",
    "\n",
    "As one can easily see, the cost function tries to balance the complexity of the data $P(\\mathcal{D}\\ |\\ \\mathbf{w})$ and the simplicity of the prior $P(\\mathbf{w})$.\n",
    "\n",
    "We can approximate this exact cost through a Monte Carlo sampling procedure as follows\n",
    "\n",
    "\\begin{equation*}\n",
    "\\mathcal{F}(\\mathcal{D}, \\mathbf{\\theta}) \\approx \\sum_{i = 1}^{n} \\log q(\\mathbf{w}^{(i)}\\ |\\ \\mathbf{\\theta}) - \\log P(\\mathbf{w}^{(i)}) - \\log P(\\mathcal{D}\\ |\\ \\mathbf{w}^{(i)})\n",
    "\\end{equation*}\n",
    "\n",
    "where $\\mathbf{w}^{(i)}$ corresponds to the $i$-th Monte Carlo sample from the variational posterior. While writing this notebook, we noticed that even taking just one sample leads to good results and we will therefore stick to just sampling once throughout the notebook.\n",
    "\n",
    "Since we will be working with mini-batches, the exact loss form on mini-batch $i$ we will be using looks as follows:\n",
    "\n",
    "\\begin{equation*}\n",
    "\\begin{split}\n",
    "\\mathcal{F}(\\mathcal{D}_i, \\mathbf{\\theta}) & = \\frac{1}{M} \\text{KL}[\\log q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})\\ ||\\ \\log P(\\mathbf{w})] - \\mathbb{E}_{q(\\mathbf{w}\\ |\\ \\mathbf{\\theta})}[\\log P(\\mathcal{D}_i\\ |\\ \\mathbf{w})]\\\\\n",
    "& \\approx \\frac{1}{M} (\\log q(\\mathbf{w}^{(1)}\\ |\\ \\mathbf{\\theta}) - \\log P(\\mathbf{w}^{(1)})) - \\log P(\\mathcal{D}_i\\ |\\ \\mathbf{w}^{(1)})\n",
    "\\end{split}\n",
    "\\end{equation*}\n",
    "\n",
    "where $M$ corresponds to the number of batches,\n",
    "and $\\mathcal{F}(\\mathcal{D}, \\mathbf{\\theta}) = \\sum_{i = 1}^{M} \\mathcal{F}(\\mathcal{D}_i, \\mathbf{\\theta})$\n",
    "\n",
    "Let's now look at each of these single terms individually."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Likelihood\n",
    "\n",
    "As with lots of past examples, we will again use the softmax to define our likelihood $P(\\mathcal{D}_i\\ |\\ \\mathbf{w})$. Revisit the [MLP from scratch notebook](https://github.com/zackchase/mxnet-the-straight-dope/blob/master/chapter03_deep-neural-networks/mlp-scratch.ipynb) for a detailed motivation of this function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def log_softmax_likelihood(yhat_linear, y):\n",
    "    return nd.nansum(y * nd.log_softmax(yhat_linear), axis=0, exclude=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prior\n",
    "\n",
    "Since we are introducing a Bayesian treatment for the network, we need to define a Prior over the weights.\n",
    "\n",
    "#### Gaussian prior\n",
    "\n",
    "A popular and simple prior is the Gaussian distribution. The prior over the entire weight vector simply corresponds to the prodcut of the individual Gaussians:\n",
    "\n",
    "\\begin{equation*}\n",
    "P(\\mathbf{w}) = \\prod_i \\mathcal{N}(\\mathbf{w}_i\\ |\\ 0,\\sigma_p^2)\n",
    "\\end{equation*}\n",
    "\n",
    "We can define the Gaussian distribution and our Gaussian prior as seen below. Note that we are ultimately intersted in the log-prior $\\log P(\\mathbf{w})$ and therefore compute the sum of the log-Gaussians.\n",
    "\n",
    "\\begin{equation*}\n",
    "\\log P(\\mathbf{w}) = \\sum_i \\log \\mathcal{N}(\\mathbf{w}_i\\ |\\ 0,\\sigma_p^2)\n",
    "\\end{equation*}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "LOG2PI = np.log(2.0 * np.pi)\n",
    "\n",
    "def log_gaussian(x, mu, sigma):\n",
    "    return -0.5 * LOG2PI - nd.log(sigma) - (x - mu) ** 2 / (2 * sigma ** 2)\n",
    "\n",
    "def gaussian_prior(x):\n",
    "    sigma_p = nd.array([config['sigma_p']], ctx=ctx)\n",
    "    \n",
    "    return nd.sum(log_gaussian(x, 0., sigma_p))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Scale mixture prior\n",
    "\n",
    "Instead of a single Gaussian, the paper also suggests the usage of a scale mixture prior for $P(\\mathbf{w})$ as an alternative:\n",
    "\n",
    "\\begin{equation*}\n",
    "P(\\mathbf{w}) = \\prod_i \\bigg ( \\pi \\mathcal{N}(\\mathbf{w}_i\\ |\\ 0,\\sigma_1^2) + (1 - \\pi) \\mathcal{N}(\\mathbf{w}_i\\ |\\ 0,\\sigma_2^2)\\bigg )\n",
    "\\end{equation*}\n",
    "\n",
    "where $\\pi \\in [0, 1]$, $\\sigma_1 > \\sigma_2$ and $\\sigma_2 \\ll 1$. Again we are intersted in the log-prior $\\log P(\\mathbf{w})$, which can be expressed as follows:\n",
    "\n",
    "\\begin{equation*}\n",
    "\\log P(\\mathbf{w}) = \\sum_i \\log \\bigg ( \\pi \\mathcal{N}(\\mathbf{w}_i\\ |\\ 0,\\sigma_1^2) + (1 - \\pi) \\mathcal{N}(\\mathbf{w}_i\\ |\\ 0,\\sigma_2^2)\\bigg )\n",
    "\\end{equation*}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def gaussian(x, mu, sigma):\n",
    "    scaling = 1.0 / nd.sqrt(2.0 * np.pi * (sigma ** 2))\n",
    "    bell = nd.exp(- (x - mu) ** 2 / (2.0 * sigma ** 2))\n",
    "    \n",
    "    return scaling * bell\n",
    "\n",
    "def scale_mixture_prior(x):\n",
    "    sigma_p1 = nd.array([config['sigma_p1']], ctx=ctx)\n",
    "    sigma_p2 = nd.array([config['sigma_p2']], ctx=ctx)\n",
    "    pi = config['pi']\n",
    "    \n",
    "    first_gaussian = pi * gaussian(x, 0., sigma_p1)\n",
    "    second_gaussian = (1 - pi) * gaussian(x, 0., sigma_p2)\n",
    "    \n",
    "    return nd.log(first_gaussian + second_gaussian)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Variational Posterior\n",
    "\n",
    "The last missing piece in the equation is the variational posterior. Again, we choose a Gaussian disribution for this purpose. The variational posterior on the weights is centered on the mean vector $\\mathbf{\\mu}$ and has variance $\\mathbf{\\sigma}^2$:\n",
    "\n",
    "\\begin{equation*}\n",
    "q(\\mathbf{w}\\ |\\ \\theta) = \\prod_i \\mathcal{N}(\\mathbf{w}_i\\ |\\ \\mathbf{\\mu},\\mathbf{\\sigma}^2)\n",
    "\\end{equation*}\n",
    "\n",
    "The log-posterior $\\log q(\\mathbf{w}\\ |\\ \\theta)$ is given by:\n",
    "\n",
    "\\begin{equation*}\n",
    "\\log q(\\mathbf{w}\\ |\\ \\theta) = \\sum_i \\log \\mathcal{N}(\\mathbf{w}_i\\ |\\ \\mathbf{\\mu},\\mathbf{\\sigma}^2)\n",
    "\\end{equation*}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Combined Loss\n",
    "\n",
    "After introducing the data likelihood, the prior, and the variational posterior, we are now able to build our combined loss function: $\\mathcal{F}(\\mathcal{D}_i, \\mathbf{\\theta}) = \\frac{1}{M} (\\log q(\\mathbf{w}\\ |\\ \\mathbf{\\theta}) - \\log P(\\mathbf{w})) - \\log P(\\mathcal{D}_i\\ |\\ \\mathbf{w})$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def combined_loss(output, label_one_hot, params, mus, sigmas, log_prior, log_likelihood):\n",
    "    \n",
    "    # Calculate data likelihood\n",
    "    log_likelihood_sum = nd.sum(log_likelihood(output, label_one_hot))\n",
    "    \n",
    "    # Calculate prior\n",
    "    log_prior_sum = sum([nd.sum(log_prior(param)) for param in params])\n",
    "\n",
    "    # Calculate variational posterior\n",
    "    log_var_posterior_sum = sum([nd.sum(log_gaussian(params[i], mus[i], sigmas[i])) for i in range(len(params))])\n",
    "    \n",
    "    # Calculate total loss\n",
    "    return 1.0 / num_batches * (log_var_posterior_sum - log_prior_sum) - log_likelihood_sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Optimizer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use vanilla stochastic gradient descent to optimize the variational parameters. Note that this implements the updates described in the paper, as the gradient contribution due to the reparametrization trick is automatically included by taking the gradients of the combined loss function with respect to the variational parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def SGD(params, lr):\n",
    "    for param in params:\n",
    "        param[:] = param - lr * param.grad"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Evaluation metric"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to being able to assess our model performance we define a helper function which evaluates our accuracy on an ongoing basis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def evaluate_accuracy(data_iterator, net, layer_params):\n",
    "    numerator = 0.\n",
    "    denominator = 0.\n",
    "    for i, (data, label) in enumerate(data_iterator):\n",
    "        data = data.as_in_context(ctx).reshape((-1, 784))\n",
    "        label = label.as_in_context(ctx)\n",
    "        output = net(data, layer_params)\n",
    "        predictions = nd.argmax(output, axis=1)\n",
    "        numerator += nd.sum(predictions == label)\n",
    "        denominator += data.shape[0]\n",
    "    return (numerator / denominator).asscalar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Parameter initialization\n",
    "\n",
    "We are using a Gaussian distribution for each individual weight as our variational posterior, which means that we need to store two parameters, mean and variance, for each weight. For the variance we need to ensure that it is non-negative, which we will do by using the softplus function to express $\\mathbf{\\sigma}$ in terms of an unconstrained parameter $\\mathbf{\\rho}$. While gradient descent will be performed on $\\theta = (\\mathbf{\\mu}, \\mathbf{\\rho})$, the distribution for each individual weight is represented as $w_i \\sim \\mathcal{N}(w_i\\ |\\ \\mu_i,\\sigma_i)$ with $\\sigma_i = \\text{softplus}(\\mathbf{\\rho}_i)$.\n",
    "\n",
    "We initialize $\\mathbf{\\mu}$ with a Gaussian around $0$ (just as we would initialize standard weights of a neural network). It is important to initialize $\\mathbf{\\rho}$ (and hence $\\sigma$) to a small value, otherwise learning might not work properly."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "weight_scale = .1\n",
    "rho_offset = -3\n",
    "\n",
    "# initialize variational parameters; mean and variance for each weight\n",
    "mus = []\n",
    "rhos = []\n",
    "    \n",
    "for shape in layer_param_shapes:\n",
    "    mu = nd.random_normal(shape=shape, ctx=ctx, scale=weight_scale)\n",
    "    rho = rho_offset + nd.zeros(shape=shape, ctx=ctx)\n",
    "    mus.append(mu)\n",
    "    rhos.append(rho)\n",
    "\n",
    "variational_params = mus + rhos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since these are the parameters we wish to do gradient descent on, we need to allocate space for storing the gradients."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for param in variational_params:\n",
    "    param.attach_grad()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Main training loop\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The main training loop is pretty similar to the one we used in the MLP example. The only adaptation we need to make is to add the weight sampling which is performed during each optimization step. Generating a set of weights, which will subsequently be used in the neural network and the loss function, is a 3-step process:\n",
    "\n",
    "1) Sample $\\mathbf{\\epsilon} \\sim \\mathcal{N}(\\mathbf{0},\\mathbf{I}^{d})$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def sample_epsilons(param_shapes):\n",
    "    epsilons = [nd.random_normal(shape=shape, loc=0., scale=1.0, ctx=ctx) for shape in param_shapes]\n",
    "    return epsilons"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2) Transform $\\mathbf{\\rho}$ to a postive vector via the softplus function: $\\mathbf{\\sigma} = \\text{softplus}(\\mathbf{\\rho}) = \\log(1 + \\exp(\\mathbf{\\rho}))$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def softplus(x):\n",
    "    return nd.log(1. + nd.exp(x))\n",
    "\n",
    "def transform_rhos(rhos):\n",
    "    return [softplus(rho) for rho in rhos]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3) Compute $\\mathbf{w}$: $\\mathbf{w} = \\mathbf{\\mu} + \\mathbf{\\sigma} \\circ \\mathbf{\\epsilon}$, where the $\\circ$ operator represents the element-wise multiplication. This is the \"reparametrization trick\" for separating the randomness from the parameters of $q$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def transform_gaussian_samples(mus, sigmas, epsilons):\n",
    "    samples = []\n",
    "    for j in range(len(mus)):\n",
    "        samples.append(mus[j] + sigmas[j] * epsilons[j])\n",
    "    return samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Complete loop\n",
    "\n",
    "The complete training loop is given below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 0. Loss: 2626.47417991, Train_acc 0.945617, Test_acc 0.9455\n",
      "Epoch 1. Loss: 2606.28165139, Train_acc 0.962783, Test_acc 0.9593\n",
      "Epoch 2. Loss: 2600.2452303, Train_acc 0.969783, Test_acc 0.9641\n",
      "Epoch 3. Loss: 2595.75639899, Train_acc 0.9753, Test_acc 0.9684\n",
      "Epoch 4. Loss: 2592.98582057, Train_acc 0.978633, Test_acc 0.9723\n",
      "Epoch 5. Loss: 2590.05895182, Train_acc 0.980483, Test_acc 0.9733\n",
      "Epoch 6. Loss: 2588.57918775, Train_acc 0.9823, Test_acc 0.9756\n",
      "Epoch 7. Loss: 2586.00932367, Train_acc 0.984, Test_acc 0.9749\n",
      "Epoch 8. Loss: 2585.4614887, Train_acc 0.985883, Test_acc 0.9765\n",
      "Epoch 9. Loss: 2582.92995846, Train_acc 0.9878, Test_acc 0.9775\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAD9CAYAAABQvqc9AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8VOW9x/HPLwkJEJZs7GQjbEZlDTsKola9dddaRVF7\nXdpbra3ebrb32tYutrfW2lvtgkoLrtfaRVu1rigKYQmbspOEbOxJSAgJIcs8948zQEhRAkxyksz3\n/Xrxysw5ZzK/GZLvPHnOc57HnHOIiEh4iPC7ABERaTsKfRGRMKLQFxEJIwp9EZEwotAXEQkjCn0R\nkTDSotA3s4vNbLOZ5ZrZt4+zP9XM3jGzj8zsPTMb3GTfz8xsXfDf50NZvIiInJwThr6ZRQKPA5cA\nmcANZpbZ7LCHgQXOuVHAg8BDwcd+FhgHjAEmAV83s16hK19ERE5GS1r6E4Fc51y+c64OeAG4otkx\nmcC7wdsLm+zPBBY55xqcc9XAR8DFp1+2iIicipaE/iCguMn9kuC2ptYCVwdvXwX0NLPE4PaLzay7\nmSUB5wHJp1eyiIicqqgQfZ+vA4+Z2a3AImA70Oice9PMJgBLgL1ANtDY/MFmdidwJ0BsbOz4kSNH\nhqgsEZHwsHLlylLnXJ8THdeS0N/Osa3zwcFtRzjndhBs6ZtZD+Aa51xFcN+PgR8H9z0HbGn+BM65\nucBcgKysLJeTk9OCskRE5DAzK2zJcS3p3lkBDDOzdDOLBq4HXmn2ZElmdvh73Q/MC26PDHbzYGaj\ngFHAmy17CSIiEmonbOk75xrM7G7gDSASmOecW29mDwI5zrlXgJnAQ2bm8Lp37go+vAvwgZkB7Adu\ncs41hP5liI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Cj\n30W0E78C/umcGwmMJozfFzMbBNwDZDnnzgIigev9rap1dYrQByYCuc65fOdcHfACcIXPNfnGObfT\nObcqeLsK75d6kL9V+cfMBgOfBZ70uxa/mVlv4FzgKQDnXJ1zrsLfqnwXBXQzsyigO7DD53paVWcJ\n/UFAcZP7JYRxyDVlZmnAWGCZv5X46lHgm0DA70LagXRgL/CHYHfXk2YW63dRfnHObQceBoqAnUCl\nc+5Nf6tqXZ0l9OU4zKwH8Gfga865/X7X4wczuxTY45xb6Xct7UQUMA74rXNuLFANhO05MDOLx+sV\nSAcGArFmdpO/VbWuzhL624HkJvcHB7eFLTPrghf4zzrn/uJ3PT6aBlxuZgV43X6zzOwZf0vyVQlQ\n4pw7/JffS3gfAuHqAmCbc26vc64e+Asw1eeaWlVnCf0VwDAzSzezaLwTMa/4XJNvzMzw+mw3Ouce\n8bsePznn7nfODXbOpeH9XLzrnOvULblP45zbBRSb2YjgpvOBDT6W5LciYLKZdQ/+3pxPJz+xHeV3\nAaHgnGsws7uBN/DOvs9zzq33uSw/TQPmAB+b2Zrgtu84517zsSZpP74CPBtsIOUDX/C5Ht8455aZ\n2UvAKrxRb6vp5Ffn6opcEZEw0lm6d0REpAUU+iIiYUShLyISRhT6IiJhRKEvIhJGFPoiImFEoS8i\nEkYU+iIiYeT/AUSFzzyvHHEbAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1096f1630>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "epochs = config['epochs']\n",
    "learning_rate = config['learning_rate']\n",
    "smoothing_constant = .01\n",
    "train_acc = []\n",
    "test_acc = []\n",
    "\n",
    "for e in range(epochs):\n",
    "    for i, (data, label) in enumerate(train_data):\n",
    "        data = data.as_in_context(ctx).reshape((-1, 784))\n",
    "        label = label.as_in_context(ctx)\n",
    "        label_one_hot = nd.one_hot(label, 10)\n",
    "        \n",
    "        with autograd.record():\n",
    "            # sample epsilons from standard normal\n",
    "            epsilons = sample_epsilons(layer_param_shapes)\n",
    "            \n",
    "            # compute softplus for variance\n",
    "            sigmas = transform_rhos(rhos)\n",
    "\n",
    "            # obtain a sample from q(w|theta) by transforming the epsilons\n",
    "            layer_params = transform_gaussian_samples(mus, sigmas, epsilons)\n",
    "            \n",
    "            # forward-propagate the batch\n",
    "            output = net(data, layer_params)\n",
    "            \n",
    "            # calculate the loss\n",
    "            loss = combined_loss(output, label_one_hot, layer_params, mus, sigmas, gaussian_prior, log_softmax_likelihood)\n",
    "            \n",
    "        # backpropagate for gradient calculation\n",
    "        loss.backward()\n",
    "        \n",
    "        # apply stochastic gradient descent to variational parameters\n",
    "        SGD(variational_params, learning_rate)\n",
    "        \n",
    "        # calculate moving loss for monitoring convergence\n",
    "        curr_loss = nd.mean(loss).asscalar()\n",
    "        moving_loss = (curr_loss if ((i == 0) and (e == 0)) \n",
    "                       else (1 - smoothing_constant) * moving_loss + (smoothing_constant) * curr_loss)\n",
    "\n",
    "        \n",
    "    test_accuracy = evaluate_accuracy(test_data, net, mus)\n",
    "    train_accuracy = evaluate_accuracy(train_data, net, mus)\n",
    "    train_acc.append(np.asscalar(train_accuracy))\n",
    "    test_acc.append(np.asscalar(test_accuracy))\n",
    "    print(\"Epoch %s. Loss: %s, Train_acc %s, Test_acc %s\" %\n",
    "          (e, moving_loss, train_accuracy, test_accuracy))\n",
    "    \n",
    "plt.plot(train_acc)\n",
    "plt.plot(test_acc)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For demonstration purposes, we can now take a look at one particular weight by plotting its distribution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW8AAAD8CAYAAAC4uSVNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl8U9ed9/HPT5J3Y+N9A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Pf28XxhiHgc9B3/G8pcDIgV9tnmEjx4YfN5zuYCPHhA5ge8OWA4lV9rFHPQc6KkE\nkn34Gvabrde2v+PjByw/9toGSLYkIMpzORU4Rp+DY8Pwml4sfBO9+UwEQK5A2GcTe11eARR5Lvvt\n83nFzEP8D6cAb3h25JaLLzI9X3ee6LXdO0A90E5Pf9Biz/XjgJ30dPC/cPHF8dGL4W22hzzPXwZ8\n1nNdHLAbKAYOAj/xxYsBLAOOet6c3/Zc913gDs/laM9+KPPsl3G97vttz/2OAEt9+iYYZC7gE579\nsw/YA6zw+Ru0/2xXed5TF+j5lnLwSq9tIGQDrgVKPB/4EuBzFmTbAtR6Xrt9wCvDsd8GmytA9tlP\ner3ft9KruPvz83m5H51hqZRSQUhnWCqlVBDS4q2UUkFIi7dSSgUhLd5KKRWEtHgrpVQQ0uKtlFJB\nSIu3UkoFIS3eSikVhP4/6wFSm8ylAfcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10e316c18>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "def show_weight_dist(mean, variance):\n",
    "    sigma = nd.sqrt(variance)\n",
    "    x = np.linspace(mean.asscalar() - 4*sigma.asscalar(), mean.asscalar() + 4*sigma.asscalar(), 100)\n",
    "    plt.plot(x, gaussian(nd.array(x, ctx=ctx), mean, sigma).asnumpy())\n",
    "    plt.show()\n",
    "    \n",
    "mu = mus[0][0][0]\n",
    "var = softplus(rhos[0][0][0]) ** 2\n",
    "\n",
    "show_weight_dist(mu, var)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great! We have obtained a fully functional Bayesian neural network. However, the number of weights now is twice as high as for traditional neural networks. As we will see in the final section of this notebook, we are able to drastically reduce the number of weights our network uses for prediction with _weight pruning_."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Weight pruning\n",
    "\n",
    "To measure the degree of redundancy present in the trained network and to reduce the model's parameter count, we now want to examine the effect of setting some of the weights to $0$ and evaluate the test accuracy afterwards. We can achieve this by ordering the weights according to their signal-to-noise-ratio, $\\frac{|\\mu_i|}{\\sigma_i}$, and setting a certain percentage of the weights with the lowest ratios to $0$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can calculate the signal-to-noise-ratio as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def signal_to_noise_ratio(mus, sigmas):\n",
    "    sign_to_noise = []\n",
    "    for j in range(len(mus)):\n",
    "        sign_to_noise.extend([nd.abs(mus[j]) / sigmas[j]])\n",
    "    return sign_to_noise"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We further introduce a few helper methods which turn our list of weights into a single vector containing all weights. This will make our subsequent actions easier."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def vectorize_matrices_in_vector(vec):\n",
    "    for i in range(0, (num_layers + 1) * 2, 2):\n",
    "        if i == 0:\n",
    "            vec[i] = nd.reshape(vec[i], num_inputs * num_hidden)\n",
    "        elif i == num_layers * 2:\n",
    "            vec[i] = nd.reshape(vec[i], num_hidden * num_outputs)\n",
    "        else:\n",
    "            vec[i] = nd.reshape(vec[i], num_hidden * num_hidden)\n",
    "            \n",
    "    return vec\n",
    "\n",
    "def concact_vectors_in_vector(vec):\n",
    "    concat_vec = vec[0]\n",
    "    for i in range(1, len(vec)):\n",
    "        concat_vec = nd.concat(concat_vec, vec[i], dim=0)\n",
    "    \n",
    "    return concat_vec\n",
    "\n",
    "def transform_vector_structure(vec):\n",
    "    vec = vectorize_matrices_in_vector(vec)\n",
    "    vec = concact_vectors_in_vector(vec)\n",
    "    \n",
    "    return vec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In addition, we also have a helper method which transforms the pruned weight vector back to the original layered structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from functools import reduce\n",
    "import operator\n",
    "\n",
    "def prod(iterable):\n",
    "    return reduce(operator.mul, iterable, 1)\n",
    "\n",
    "def restore_weight_structure(vec):\n",
    "    pruned_weights = []\n",
    "    \n",
    "    index = 0\n",
    "    \n",
    "    for shape in layer_param_shapes:\n",
    "        incr = prod(shape)\n",
    "        pruned_weights.extend([nd.reshape(vec[index : index + incr], shape)])\n",
    "        index += incr\n",
    "    \n",
    "    return pruned_weights"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The actual pruning of the vector happens in the following function. Note that this function accepts an ordered list of percentages to evaluate the performance at different pruning rates. In this setting, pruning at each iteration means extracting the index of the lowest signal-to-noise-ratio weight and setting the weight at this index to $0$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true,
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "def prune_weights(sign_to_noise_vec, prediction_vector, percentages):\n",
    "    pruning_indices = nd.argsort(sign_to_noise_vec, axis=0)\n",
    "    \n",
    "    for percentage in percentages:\n",
    "        prediction_vector = mus_copy_vec.copy()\n",
    "        pruning_indices_percent = pruning_indices[0:int(len(pruning_indices)*percentage)]\n",
    "        for pr_ind in pruning_indices_percent:\n",
    "            prediction_vector[int(pr_ind.asscalar())] = 0\n",
    "        pruned_weights = restore_weight_structure(prediction_vector)\n",
    "        test_accuracy = evaluate_accuracy(test_data, net, pruned_weights)\n",
    "        print(\"%s --> %s\" % (percentage, test_accuracy))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Putting the above functions together:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1 --> 0.9777\n",
      "0.25 --> 0.9779\n",
      "0.5 --> 0.9756\n",
      "0.75 --> 0.9602\n",
      "0.95 --> 0.7259\n",
      "0.99 --> 0.3753\n",
      "1.0 --> 0.098\n"
     ]
    }
   ],
   "source": [
    "sign_to_noise = signal_to_noise_ratio(mus, sigmas)\n",
    "sign_to_noise_vec = transform_vector_structure(sign_to_noise)\n",
    "\n",
    "mus_copy = mus.copy()\n",
    "mus_copy_vec = transform_vector_structure(mus_copy)\n",
    "\n",
    "prune_weights(sign_to_noise_vec, mus_copy_vec, [0.1, 0.25, 0.5, 0.75, 0.95, 0.99, 1.0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Depending on the number of units used in the original network and the number of training epochs, the highest achievable pruning percentages (without significantly reducing the predictive performance) can vary. The paper, for example, reports almost no change in the test accuracy when pruning 95% of the weights in a 2x1200 unit Bayesian neural network, which creates a significantly sparser network, leading to faster predictions and reduced memory requirements."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "We have taken a look at an efficient Bayesian treatment for neural networks using variational inference via the \"Bayes by Backprop\" algorithm (introduced by the \"[Weight Uncertainity in Neural Networks](https://arxiv.org/abs/1505.05424)\" paper). We have implemented a stochastic version of the variational lower bound and optimized it in order to find an approximation to the posterior distribution over the weights of a MLP network on the MNIST data set. As a result, we achieve regularization on the network's parameters and can quantify our uncertainty about the weights accurately. Finally, we saw that it is possible to significantly reduce the number of weights in the neural network after training while still keeping a high accuracy on the test set.\n",
    "\n",
    "We also note that, given this model implementation, we were able to reproduce the paper's results on the MNIST data set, achieving a comparable test accuracy for all documented instances of the MNIST classification problem."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For whinges or inquiries, [open an issue on  GitHub.](https://github.com/zackchase/mxnet-the-straight-dope)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
